package com.lx.blogflux.util;

import com.lx.blogflux.dao.biz.BizException;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Utils {

    public static void check(boolean bool, String msg) throws BizException {
        if(bool) throw new BizException(msg);
    }

    public static void checkEmpty(Object value, String msg) throws BizException {
        check(isEmpty(value), msg);
    }

    // 判断是否为空
    public static boolean isEmpty(Object value) {
        boolean bool = false;
        if (value == null) bool = true;
        else if (value instanceof String) {
            bool = ((String) value).isBlank();
        } else if (value instanceof Collection<?>) {
            bool = ((Collection<?>) value).isEmpty();
        } else if (value instanceof Map<?, ?>) {
            bool = ((Map<?, ?>) value).isEmpty();
        }
        return bool;
    }
    /**
     * 数组转 map 集合
     * @param values
     * @return
     * @param <K>
     * @param <V>
     */
    public static <K, V> Map<K, V> asMap(Object... values) {
        Map<K, V> map = new HashMap<K, V>();
        for (int i = 0; i < values.length; i += 2) {
            if (i < values.length - 1) {
                map.put((K) values[i], (V) values[i + 1]);
            } else if (i == values.length - 1) {
                map.put((K) values[i], (V) values[i]);
            }
        }
        return map;
    }

}
